﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Trans.customer;
using Trans.order;
using Trans.car;
using Infragistics.Win.UltraWinToolbars;
using Trans.member;
using Trans.system;
using Trans.session;
using Trans.depart;
using System.Runtime.InteropServices; 


namespace Trans
{

    public partial class Main : Form
    {
        [DllImport("User32.DLL")]
        public static extern void LockWorkStation();
          
        public Main()
        {
            InitializeComponent();
        }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            
            Form form=null;
            switch (e.Tool.Key)
            {
                #region 系统选项
                case "sys_退出":
                    {
                        if (
                            MessageBox.Show(
                                "确定退出?",
                                "提示",
                                MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Warning) == DialogResult.OK)
                        {
                            this.Close();
                        } 
                        break;
                    }
                case "sys_锁定系统":
                    {
                        if(
                            MessageBox.Show(
                                "确定锁定计算机?", 
                                "", 
                                MessageBoxButtons.OKCancel, 
                                MessageBoxIcon.Warning)==DialogResult.OK)
                        {
                            LockWorkStation();
                        }
                        break;
                    }
                case "sys_登出":
                    {
                        if (
                            MessageBox.Show(
                                "确定登出?",
                                "提示",
                                MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Warning) == DialogResult.OK)
                        {
                            logout();
                           
                        } 
                        break;
                    }
                case "sys_数据备份":
                    {
                        form = new Sys_backup();
                        break;
                    }
                case "sys_数据恢复":
                    {
                        form = new Sys_restore();
                        break;
                    }
                case "sys_数据清空":
                    {
                        form = new Sys_init();
                        break;
                    }

                case "sys_关于":
                    {
                        form = new Sys_about();
                        pn_main.Controls.Clear();
                        form.ShowDialog();
                        return;
                    }
  
                case "user_登录":
                    {
                        login();
                        break;
                    }
#endregion

                #region 订单管理

                case "order_新订单":    
                    {
                        form = new Add_order();
                        break;
                    }
                case "order_管理订单":
                    {
                        form = new Manage_allOrders();
                        break;
                    }
                case "order_查询订单":
                    {
                        form = new Query_allOrders();
                        break;
                    }
                case "order_订单分析":
                    {
                        form = new Analy_order();

                        break;
                    }
#endregion

                #region 客户管理

                case "customer_新增客户":
                    {
                        form = new Add_customer();
                        break;
                    }
                case "customer_简单查询":
                    {
                        form = new CQuery_customer();
                        break;
                    }
                case "customer_全部客户":
                    {
                        form = new Manage_customer();
                        break;
                    }
                case "customer_客户报表":
                    {
                        form = new Analy_customer();
                        break;
                    }
              #endregion 客户管理

                #region 车辆管理

                case "car_添加维修记录":
                    {
                        form = new Add_maintain();
                        break;
                    }
                case "car_报修单查询":
                    {
                        form = new Query_maintain();
                        break;
                    }
                case "car_维修记录管理":
                    {
                        form = new Manage_maintain();
                        break;
                    }
                case "car_报修单据":
                    {
                        form = new Analy_maintain();
                        break;
                    }
                case "car_添加事故记录":
                    {
                        form = new Add_accident();
                        break;
                    }
                case "car_查询事故记录":
                    {
                        form = new Query_accident();    
                        break;
                    }
                case "car_管理事故记录":
                    {
                        form = new Manage_accident();
                        break;

                    }
                case "car_事故记录单":
                    {
                        form = new Analy_accident();
                        break;
                    }
                case "car_添加缴费单":
                    {
                        form = new Add_charge();
                        break;
                    }
                case "car_查询缴费单":
                    {
                        form = new Query_charge();
                        break;
                    }

                case "car_管理缴费单":
                    {
                        form = new Manage_charge();
                        break;
                    }
                case "car_缴费单据":
                    {
                        form = new Analy_charge();
                        break;
                    }
                case "car_新增车辆":
                    {
                        form = new Add_car();
                        break;
                    }
                case "car_新增车型":
                    {
                        form = new Add_cartype();
                        break;
                    }
                case "car_车辆查询":
                    {
                        form = new Query_car();
                        break;
                    }
                case "car_车辆资料管理":
                    {
                        form = new Manage_car();
                        break;
                    }
                case "car_车辆报表":
                    {
                        form = new Analy_car();
                        break;
                    }
#endregion

                #region 员工管理

                case "mem_新增员工":
                    {
                        form = new Add_member();
                        break;
                    }
                case "mem_员工管理":
                    {
                        form = new Manage_member();
                        break;
                    }
                case "mem_快速查询":
                    {
                        form = new Query_member();
                        break;
                    }
                case "mem_高级查询":
                    {
                        form = new CQuery_member();
                        break;
                    }
                case "mem_设置权限":
                    {
                        form = new Manage_access();
                        break;
                    }
                case "mem_发放薪酬":
                    {
                        form = new Putout_salary();
                        break;
                    }
                case "mem_薪酬调整":
                    {
                        form = new Manage_salary();
                        break;
                    }
                case "mem_添加分部":
                    {
                        form = new Add_depart();
                        break;
                    }
                case "mem_分部查询":
                    {
                        form = new Query_depart();
                        break;
                    }
                case "mem_分部管理":
                    {
                        form = new Manage_depart();
                        break;
                    }
                case "mem_员工统计":
                    {
                        form = new Analy_member();
                        break;
                    }
                case "mem_薪酬统计":
                    {
                        form = new Analy_salary();
                        break;
                       }
                case "mem_分部统计":
                    {
                        form = new Analy_depart();
                        break;
                    }
#endregion
            }
            pn_main.Controls.Clear();
            if (form != null)
            {


                form.TopLevel = false;

                form.FormBorderStyle = FormBorderStyle.None;
                pn_main.Controls.Add(form);
                form.Show();
            }
        }
        /// <summary>
        /// 登出
        /// </summary>
        private void logout()
        {
            PopupMenuTool tl_login = (PopupMenuTool)toolBar.Tools["sys_登录"];
            tl_login.SharedProps.Enabled = true;
            ButtonTool tl_logout = (ButtonTool)toolBar.Tools["sys_登出"];
            tl_logout.SharedProps.Enabled = false;
            Init_bool_panel(false);
        }
        /// <summary>
        /// 登录
        /// </summary>
        private void login()
        {

            TextBoxTool tl_name = (TextBoxTool)toolBar.Tools["user_name"];
            TextBoxTool tl_pass = (TextBoxTool)toolBar.Tools["user_passwd"];
            if (tb_memberTableAdapter.Login(dataSet.tb_member,tl_name.Text, tl_pass.Text) > 0)
            {
                MessageBox.Show("欢迎您:" + tl_name.Text);
                
                /*保存session*/
                Oper.id = (int)dataSet.tb_member.Rows[0]["id"];
                Oper.name= (string)dataSet.tb_member.Rows[0]["name"];
                Oper.time = DateTime.Now;
                Init_bool_panel(true);
                Init_access_panel();
                
                
               
            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
        }   
        /// <summary>
        /// 根据权限，初始化界面
        /// </summary>
        private void Init_access_panel()
        {
            /*状态栏*/
            statusBar.Panels[0].Text = "操作员:" + " " + Oper.name;
    
            statusBar.Panels[1].Text = "登录时间： " + Oper.time.ToShortTimeString();
               
            PopupMenuTool tl_login = (PopupMenuTool)toolBar.Tools["sys_登录"];
            tl_login.SharedProps.Enabled = false;
            ButtonTool tl_logout = (ButtonTool)toolBar.Tools["sys_登出"];
            tl_logout.SharedProps.Enabled = true;
              
            /*权限*/
            tb_opertorTableAdapter.FillById(dataSet.tb_opertor, Oper.id);
            byte ac;
            ButtonTool tl;
            #region 成员
            ac = (byte)(dataSet.tb_opertor.Rows[0]["mem_manage"]);
            if ((ac & Access_Const.ACCESS_ADD) ==0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_新增员工"];
                tl.SharedProps.Enabled = false;
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_MANAGE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_员工管理"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_QUERY) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_快速查询"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["mem_高级查询"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["mem_员工统计"];
                tl.SharedProps.Visible = false;
            }
            #endregion

            #region 车辆资料
            ac = (byte)(dataSet.tb_opertor.Rows[0]["car_manage"]);
            if ((ac & Access_Const.ACCESS_ADD) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_新增车辆"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["car_新增车型"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_MANAGE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_车辆资料管理"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_QUERY) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_车辆报表"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["car_车辆查询"];
                tl.SharedProps.Visible = false;
            }
#endregion

            #region 订单
            ac = (byte)(dataSet.tb_opertor.Rows[0]["order_manage"]);
            if ((ac & Access_Const.ACCESS_ADD) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["order_新订单"];
                tl.SharedProps.Enabled = false;
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_MANAGE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["order_管理订单"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_QUERY) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["order_查询订单"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["order_订单分析"];
                tl.SharedProps.Visible = false;
            }
            #endregion
                
            #region 报修单
            ac = (byte)(dataSet.tb_opertor.Rows[0]["maintain_manage"]);
            if ((ac & Access_Const.ACCESS_ADD) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_添加维修记录"];
                tl.SharedProps.Enabled = false;
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_MANAGE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_维修记录管理"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_QUERY) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_报修单查询"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["car_报修单据"];
                tl.SharedProps.Visible = false;
            }
            #endregion

            #region 缴费单
            ac = (byte)(dataSet.tb_opertor.Rows[0]["charge_manage"]);
            if ((ac & Access_Const.ACCESS_ADD) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_添加缴费单"];
                tl.SharedProps.Enabled = false;
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_MANAGE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_管理缴费单"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_QUERY) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_查询缴费单"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["car_缴费单据"];
                tl.SharedProps.Visible = false;
            }
            #endregion

            #region 事故
            ac = (byte)(dataSet.tb_opertor.Rows[0]["accident_manage"]);
            if ((ac & Access_Const.ACCESS_ADD) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_添加事故记录"];
                tl.SharedProps.Enabled = false;
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_MANAGE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_管理事故记录"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_QUERY) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["car_查询事故记录"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["car_事故记录单"];
                tl.SharedProps.Visible = false;
            }
            #endregion

            #region 薪酬
                         
            ac = (byte)(dataSet.tb_opertor.Rows[0]["salary_manage"]);
            if ((ac & Access_Const.ACCESS_ADD) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_发放薪酬"];
                
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_MANAGE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_薪酬调整"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_QUERY) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_薪酬统计"];
                tl.SharedProps.Visible = false;
            }
            #endregion 

            #region 客户
            ac = (byte)(dataSet.tb_opertor.Rows[0]["customer_manage"]);
            if ((ac & Access_Const.ACCESS_ADD) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["customer_新增客户"];
                tl.SharedProps.Enabled = false;
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_MANAGE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["customer_全部客户"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_QUERY) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["customer_简单查询"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["customer_客户报表"];
                tl.SharedProps.Visible = false;
            }
            #endregion

            #region 分部

            ac = (byte)(dataSet.tb_opertor.Rows[0]["depart_manage"]);
            if ((ac & Access_Const.ACCESS_ADD) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_添加分部"];
                tl.SharedProps.Enabled = false;
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_MANAGE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_分部管理"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.ACCESS_QUERY) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_分部查询"];
                tl.SharedProps.Visible = false;
                tl = (ButtonTool)toolBar.Tools["mem_分部统计"];
                tl.SharedProps.Visible = false;
            }
            #endregion

            #region 系统
            ac = (byte)(dataSet.tb_opertor.Rows[0]["sys_manage"]);
            if((ac&Access_Const.SYS_ACCESS_BACKUP)==0)
            {
                tl = (ButtonTool)toolBar.Tools["sys_数据备份"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.SYS_ACCESS_RESTORE) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["sys_数据恢复"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.SYS_ACCESS_SETACCESS) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["mem_设置权限"];
                tl.SharedProps.Visible = false;
            }
            if ((ac & Access_Const.SYS_ACCESS_INIT) == 0)
            {
                tl = (ButtonTool)toolBar.Tools["sys_数据清空"];
                tl.SharedProps.Visible = false;
            }
            #endregion
        }

        /// <summary>
        /// 游客面板初始化
        /// <param name="b">
        ///     面板是否显示
        /// </param>
        /// </summary>
        private void Init_bool_panel(bool b)
        {
            b = true;
            ButtonTool tl;
            #region 成员
           
                tl = (ButtonTool)toolBar.Tools["mem_新增员工"];
              
                tl.SharedProps.Visible = b;
           
         
                tl = (ButtonTool)toolBar.Tools["mem_员工管理"];
                tl.SharedProps.Visible = b;
          
                tl = (ButtonTool)toolBar.Tools["mem_快速查询"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["mem_高级查询"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["mem_员工统计"];
                tl.SharedProps.Visible = b;

            #endregion

            #region 车辆资料
        
                tl = (ButtonTool)toolBar.Tools["car_新增车辆"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_新增车型"];
                tl.SharedProps.Visible = b;
          
                tl = (ButtonTool)toolBar.Tools["car_车辆资料管理"];
                tl.SharedProps.Visible = b;
            
              tl = (ButtonTool)toolBar.Tools["car_车辆报表"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_车辆查询"];
                tl.SharedProps.Visible = b;
            
            #endregion

            #region 订单
            
                tl = (ButtonTool)toolBar.Tools["order_新订单"];
                tl.SharedProps.Enabled = b;
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["order_管理订单"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["order_查询订单"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["order_订单分析"];
                tl.SharedProps.Visible = b;
          
            #endregion

            #region 报修单
                tl = (ButtonTool)toolBar.Tools["car_添加维修记录"];
                
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_维修记录管理"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_报修单查询"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_报修单据"];
                tl.SharedProps.Visible = b;
            
            #endregion

            #region 缴费单
                tl = (ButtonTool)toolBar.Tools["car_添加缴费单"];
             
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_管理缴费单"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_查询缴费单"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_缴费单据"];
                tl.SharedProps.Visible = b;
            
            #endregion

            #region 事故
                tl = (ButtonTool)toolBar.Tools["car_添加事故记录"];
             
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_管理事故记录"];
                tl.SharedProps.Visible = b;
           
                tl = (ButtonTool)toolBar.Tools["car_查询事故记录"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["car_事故记录单"];
                tl.SharedProps.Visible = b;
            
            #endregion

            #region 薪酬

           tl = (ButtonTool)toolBar.Tools["mem_发放薪酬"];

                tl.SharedProps.Visible = b;
             tl = (ButtonTool)toolBar.Tools["mem_薪酬调整"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["mem_薪酬统计"];
                tl.SharedProps.Visible = b;
            #endregion

            #region 客户
             tl = (ButtonTool)toolBar.Tools["customer_新增客户"];
              
                tl.SharedProps.Visible = b;
            
           
                tl = (ButtonTool)toolBar.Tools["customer_全部客户"];
                tl.SharedProps.Visible = b;
            tl = (ButtonTool)toolBar.Tools["customer_简单查询"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["customer_客户报表"];
                tl.SharedProps.Visible = b;
         
            #endregion

            #region 分部

            tl = (ButtonTool)toolBar.Tools["mem_添加分部"];
               
                tl.SharedProps.Visible = b;
             tl = (ButtonTool)toolBar.Tools["mem_分部管理"];
                tl.SharedProps.Visible = b;
             tl = (ButtonTool)toolBar.Tools["mem_分部查询"];
                tl.SharedProps.Visible = b;
                tl = (ButtonTool)toolBar.Tools["mem_分部统计"];
                tl.SharedProps.Visible = b;
           
            #endregion

            #region 系统
               tl = (ButtonTool)toolBar.Tools["sys_数据备份"];
                tl.SharedProps.Visible = b;
             tl = (ButtonTool)toolBar.Tools["sys_数据恢复"];
                tl.SharedProps.Visible = b;
            
             tl = (ButtonTool)toolBar.Tools["mem_设置权限"];
                tl.SharedProps.Visible = b;
            
              tl = (ButtonTool)toolBar.Tools["sys_数据清空"];
                tl.SharedProps.Visible = b;
           
            #endregion
        }



        private void Main_Load(object sender, EventArgs e)
        {
            ButtonTool bt = (ButtonTool)toolBar.Tools["sys_登出"];
            bt.SharedProps.Enabled = false;
            Init_bool_panel(false);
        }
    }
}
